///**
// * 
// */
package duckHunter.app;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.MIDlet;

import duckHunter.ui.HelpCanvas;
import duckHunter.ui.Level1;
import duckHunter.ui.ScoreCanvas;
import duckHunter.ui.SoundPlayer;
import duckHunter.ui.menuCanvas;


/**The main class of the application
 * it extends MIDlet class and implements CommandListener interface
 * 
 * @author AYARI
 * 
 */
public class DuckHunter extends MIDlet implements CommandListener {
	//canvas classes for the interaction with the user 
	//to determine actions taken
	private menuCanvas principlePage;
	//Game canvas class
	private static Level1 mCanvas;
	//canvas to view help instructions
	private HelpCanvas helpPage;
	//canvas to view score
	private ScoreCanvas scorePage;

	//commands to manage the user's navigation
	private Command exitCommand = new Command("Exit", Command.EXIT, 1);
	private Command backCommand = new Command("Back", Command.BACK, 1);
	private Command launchCommand = new Command("Launch", Command.ITEM, 1);

	//display canvas class
	private Display display;

	//for playing the music
	private SoundPlayer player;

	/**
	 * Constructor
	 */
	public DuckHunter() {
		super();		
		//initialize the display
		display = Display.getDisplay(this);

	}

	/**
	 * Called when this MIDlet is started for the first time, or when it returns
	 * from paused mode. If a player is visible, and it was playing when the
	 * MIDlet was paused, call its playSound method.
	 */
	// initialize the canvas class
	//set the principlePage canvas as display
	//add commands to canvas classes
	//and set "this" class as command listener to all canvas classes 
	public void startApp() {

		principlePage = new menuCanvas(this);
		principlePage.addCommand(launchCommand);
		principlePage.addCommand(exitCommand);
		display.setCurrent(principlePage);	

		helpPage = new HelpCanvas();
		helpPage.addCommand(backCommand);
		helpPage.setCommandListener(this);
		
		scorePage = new ScoreCanvas();
		scorePage.addCommand(backCommand);
		scorePage.setCommandListener(this);


		try {
			player = new SoundPlayer("/audio/fusil.wav");
			player.play();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MediaException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Called when this MIDlet is paused. If the player GUI is visible, call its
	 * pauseSound method. For consistency across different VM's it's a good idea
	 * to stop the player if it's currently playing.
	 */
	public void pauseApp() {
	}

	/**
	 * Destroy must cleanup everything not handled by the garbage collector.
	 */
	public void destroyApp(boolean unconditional) {
		if (mCanvas != null) {
			mCanvas.stop();
		}
	}
	/**
	 * listens to the commands if any is being fired
	 */
	public void commandAction(Command c, Displayable s) {
		// if the command is exit 
		//set principle page as displayable
		//else we destroy the application
		if (c == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} 
		
		//if command is launch command, create a new game canvas and start the game
		else if (c == launchCommand) 
		{
			if (mCanvas == null) {
				player.stop();
				try {
					mCanvas = new Level1();
				} catch (IOException e) {
					e.printStackTrace();
				}
				mCanvas.start();
				mCanvas.addCommand(backCommand);
				mCanvas.setCommandListener(this);
			}
			Display.getDisplay(this).setCurrent(mCanvas);
		} 
		// if command is backCommand
		//and the displayable is GameCanvas(mcanvas)
		//stop the game and return to principlePage(set as display)
		else if (c == backCommand) {
			if ( s==mCanvas){
				Display.getDisplay(this).setCurrent(principlePage);
				if (mCanvas != null) {
					mCanvas.setMRun(false);
					mCanvas.stop();
					mCanvas = null;
					//set the number of lives and score to their initial values
					GameContext.getInstance().setNbCartouche(20);
					GameContext.getInstance().setScore(0);
					GameContext.getInstance().updateHighScore();

				}
			}
		}//next command used to navigate in the story
		else if (s==scorePage || s==helpPage){
			display.setCurrent(principlePage);
		}
	}

	//create a Level1 gameCanvas and start the game
	public void startGame() {
		player.stop();
		try {
			mCanvas = new Level1();
			mCanvas.addCommand(backCommand);
			mCanvas.setCommandListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}

		display.setCurrent(mCanvas);
		mCanvas.repaint();
		mCanvas.start();
	}

	
	//used to resume the paused game
	public void resumeGame() {
		principlePage = null;
		display.setCurrent(mCanvas);
		mCanvas.start();

	}

	//set scorePage as displayable
	public void startHighScore() {
		player.stop();
	}

	//set the help page as displayable
	public void startHelp() {
		player.stop();
		display.setCurrent(helpPage);
		helpPage.repaint();
	}
}

